package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.event.*;
import java.sql.SQLException;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderPayment 
{
    private JPanel contentPanel;
    private OrderContainer orderContainer;
    private JTextField tFieldPayment;

    public OrderPayment(OrderContainer view)
    {
        orderContainer = view;
        
        contentPanel = new JPanel(null);
        contentPanel.setBounds(0, 0, 900, 500);
        contentPanel.setOpaque(false);
        contentPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Bogfør Betaling", 
                                TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        
        contentPanel.setVisible(true);
    }
    
    public void buildContentPanel(final model.Order order)
    {
        contentPanel.removeAll();
        
        JLabel closeLabel = new JLabel("x");
        closeLabel.setBounds(870, 25, 20, 20);
        closeLabel.setForeground(DesignLayout.T_FIELD_BORDER_RED);
        closeLabel.setFont(DesignLayout.STANDARD.deriveFont(18f));
        closeLabel.setBorder(null);
        closeLabel.addMouseListener(new MouseAdapter() 
        {
            @Override
            public void mouseReleased(MouseEvent evt)
            {
                orderContainer.getOrderPayment().getPanel().setVisible(false);
                orderContainer.getOrderSearch().getPanel().setVisible(true);
                orderContainer.getOrderSearchList().buildExistingScrollPanel("WHERE finished = 0", "ORDER BY orderID DESC", "");
                orderContainer.getOrderSearchList().getPanel().setVisible(true);
                orderContainer.getNewOrderButtonPanel().getPanel().setVisible(true);
            }
        });
        contentPanel.add(closeLabel);
        
        DecimalFormat df = new DecimalFormat("#.##");
        JLabel lblOrderNumber = new JLabel("<html><b>Ordrenummer: </b>"+order.getId()+"</html>");
        lblOrderNumber.setBounds(25, 25, 500, 35);
        lblOrderNumber.setFont(DesignLayout.STANDARD.deriveFont(22f));
        contentPanel.add(lblOrderNumber);
        
        JLabel lblCustomer = new JLabel("<html><b>Kunde: </b>"+order.getCustomerSpecified()[0].getCustomerTelephone().getNumber()+
                                                            " ("+order.getCustomerSpecified()[0].getCustomerName().getName()+")</html>");
        lblCustomer.setBounds(25, 70, 500, 35);
        lblCustomer.setFont(DesignLayout.STANDARD.deriveFont(22f));
        contentPanel.add(lblCustomer);
        
        JLabel lblOrderDate = new JLabel("<html><b>Ordredato: </b>"+order.getOrder_date()+"</html>");
        lblOrderDate.setBounds(25, 115, 500, 35);
        lblOrderDate.setFont(DesignLayout.STANDARD.deriveFont(22f));
        contentPanel.add(lblOrderDate);
        
        JLabel lblTotal = new JLabel("<html><b>Totalkøb: </b>"+df.format(order.totalPriceVat())+"</html>");
        lblTotal.setBounds(25, 160, 500, 35);
        lblTotal.setFont(DesignLayout.STANDARD.deriveFont(22f));
        contentPanel.add(lblTotal);
        
        JLabel lblReceived = new JLabel("<html><b>Modtaget: </b>"+df.format(order.getPayment_received())+"</html>");
        lblReceived.setBounds(25, 205, 500, 35);
        lblReceived.setFont(DesignLayout.STANDARD.deriveFont(22f));
        contentPanel.add(lblReceived);
        
        JLabel lblReceivePayment = new JLabel("<html><b>Modtag Beløb</b></html>", JLabel.CENTER);
        lblReceivePayment.setBounds(550, 300, 250, 35);
        lblReceivePayment.setFont(DesignLayout.STANDARD.deriveFont(22f));
        contentPanel.add(lblReceivePayment);
        
        tFieldPayment = new JTextField();
        tFieldPayment.setBorder(DesignLayout.T_FIELD_RED);
        tFieldPayment.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldPayment.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldPayment.setBounds(550, 335, 250, 35);
        tFieldPayment.setFont(DesignLayout.STANDARD.deriveFont(22f));
        if (order.isFinished() == true)
        {
            tFieldPayment.setEditable(false);
        }
        tFieldPayment.setText("" + df.format((order.totalPriceVat() - order.getPayment_received())));
        tFieldPayment.selectAll();
        tFieldPayment.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                receivePayment(order);
            }
        });
        contentPanel.add(tFieldPayment);
        
        JButton btnReceive = new JButton("modtaget");
        btnReceive.setBorder(DesignLayout.T_FIELD_RED);
        btnReceive.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnReceive.setHorizontalAlignment(SwingConstants.CENTER);
        btnReceive.setBounds(650, 380, 150, 35);
        btnReceive.setFont(DesignLayout.STANDARD.deriveFont(22f));
        if (order.isFinished() == true)
        {
            btnReceive.setEnabled(false);
        }
        btnReceive.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                receivePayment(order);
            }
        });
        contentPanel.add(btnReceive);
                
        contentPanel.revalidate();
        contentPanel.repaint();
    }
    
    private void receivePayment(model.Order order)
    {
        try
        {
            String received = tFieldPayment.getText().replace(",", ".");
            double toPay = order.totalPriceVat() - order.getPayment_received();
            int altered = control.OrderControl.receivePayment(order, received);
            if (altered > 0)
            {
                orderContainer.getOrderPayment().getPanel().setVisible(false);
                orderContainer.getOrderSearch().getPanel().setVisible(true);
                orderContainer.getOrderSearchList().buildExistingScrollPanel("WHERE orderID = "+order.getId(), "ORDER BY orderID DESC", "");
                orderContainer.getOrderSearchList().getPanel().setVisible(true);
                orderContainer.getNewOrderButtonPanel().getPanel().setVisible(true);

                if (Double.parseDouble(received) > toPay)
                {
                    double payBack = Double.parseDouble(received) - toPay;
                    if (payBack > 0)
                    {
                        DecimalFormat df = new DecimalFormat("#.##");
                        JOptionPane optionPane = new JOptionPane();
                        optionPane.showMessageDialog(null,
                        "Betaling Modtaget\n"
                        + "Modtaget: DKK "+ df.format(Double.parseDouble(received)) + "\n"
                        + "Retur: DKK "+ df.format(payBack),
                        "Betaling Modtaget",
                        JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        catch (Exception ex)
        {
            ExceptionManager.handleException(ex);
        }        
    }

    public JPanel getPanel()
    {
        return contentPanel;
    }
}